package com.ruoyi.prevention.segregater.service.impl;

import java.util.List;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.prevention.segregater.mapper.SegregaterMapper;
import com.ruoyi.prevention.segregater.domain.Segregater;
import com.ruoyi.prevention.segregater.service.ISegregaterService;

/**
 * 隔离人员信息Service业务层处理
 * 
 * @author wad
 * @date 2022-04-06
 */
@Service
public class SegregaterServiceImpl implements ISegregaterService 
{
    @Autowired
    private SegregaterMapper segregaterMapper;

    /**
     * 查询隔离人员信息
     * 
     * @param segreId 隔离人员信息主键
     * @return 隔离人员信息
     */
    @Override
    public Segregater selectSegregaterBySegreId(Long segreId)
    {
        return segregaterMapper.selectSegregaterBySegreId(segreId);
    }

    @Override
    public List<Segregater> selectSegregaterList2(Segregater segregater) {
        return segregaterMapper.selectSegregaterList2(segregater);
    }

    /**
     * 查询隔离人员信息列表
     * 
     * @param segregater 隔离人员信息
     * @return 隔离人员信息
     */
    @Override
    public List<Segregater> selectSegregaterList(Segregater segregater)
    {
        return segregaterMapper.selectSegregaterList(segregater);
    }

    /**
     * 新增隔离人员信息
     * 
     * @param segregater 隔离人员信息
     * @return 结果
     */
    @Override
    public int insertSegregater(Segregater segregater)
    {
        return segregaterMapper.insertSegregater(segregater);
    }

    /**
     * 修改隔离人员信息
     * 
     * @param segregater 隔离人员信息
     * @return 结果
     */
    @Override
    public int updateSegregater(Segregater segregater)
    {
        return segregaterMapper.updateSegregater(segregater);
    }

    /**
     * 批量删除隔离人员信息
     * 
     * @param segreIds 需要删除的隔离人员信息主键
     * @return 结果
     */
    @Override
    public int deleteSegregaterBySegreIds(Long[] segreIds)
    {
        return segregaterMapper.deleteSegregaterBySegreIds(segreIds);
    }

    /**
     * 删除隔离人员信息信息
     * 
     * @param segreId 隔离人员信息主键
     * @return 结果
     */
    @Override
    public int deleteSegregaterBySegreId(Long segreId)
    {
        return segregaterMapper.deleteSegregaterBySegreId(segreId);
    }

    /**
     * 导入教师信息实现
     * @param userList
     * @param updateSupport
     * @param operName
     * @return
     */
    @Override
    public String importUser(List<Segregater> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Segregater user : userList)
        {
            try
            {
                // 验证是否存在这个用户
                Segregater u = segregaterMapper.selectSegregaterBySegreName(user.getSegreName());
                if (StringUtils.isNull(u))
                {
                    user.setCreateBy(operName);
                    user.setSegreName(user.getSegreName());
                    segregaterMapper.insertImportSergregater(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getSegreName() + " 导入成功");
                }
                else if (updateSupport)
                {
                    user.setUpdateBy(operName);
                    this.updateSegregater(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getSegreName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " +user.getSegreName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getSegreName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
